﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication3.Purchase
{
    public partial class PurchaseApproval : Form
    {
        DataBean.PurchaseRequestDatabean purchaserequestdatabean = null;

      //  private String connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\NauferTransactionDemoDB.accdb";
        public PurchaseApproval()
        {
            InitializeComponent();
            
            showallpurchaserequest();
        }

      
        /// <summary>
        /// Displays all Purchaserequest which are not aaproved
        /// </summary>
        public void showallpurchaserequest()
        {
            
            try
            {



                String query = "Select * from purchasereportmastertable where isapproved= false ";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, Program.ConnStr);

                DataSet ds = new DataSet();
                dAdapter.Fill(ds, "tblprdataview");
                tblpurchaseorder.DataSource = ds.Tables["tblprdataview"].DefaultView;

            }
            catch (Exception)
            {
                MessageBox.Show("The application had met with some errors at displaying Pr please restart  the application :\n error:closer MSAccess files");

            }


        }

        public void updateaction(DataBean.PurchaseRequestDatabean purdatbean)
        {
            try
                       {

                           OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(Program.ConnStr);
                            oleDbConnection1.Open();
                            String query = "Update purchasereportmastertable set item='" + purdatbean.Item + "',description ='" +purdatbean.Description  + "', quantity='" + purdatbean.Quantity+ "',supplier='" + purdatbean.Supplier + "',remark='" + purdatbean.Remark+ "' ,prdate='" + purdatbean.Prdate + "',isapproved=" + purdatbean.Isaproval + ",aprroveddate='" + purdatbean.Aprovaldate + "' where prpk =" + purdatbean.Prpk + "";


                            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand(query, oleDbConnection1);
                            oleDbCommand1.CommandType = CommandType.Text;

                            oleDbCommand1.ExecuteNonQuery();

                            oleDbConnection1.Close();
                            MessageBox.Show("Sucess");
                            showallpurchaserequest();



                      }
            catch (Exception exp)
            {
               MessageBox.Show("Cannot Update values to Purchasereporttable" + exp);

            }
        }



        /// <summary>
        /// validates the control
        /// </summary>
        public void validateDatagrid()
        {

            Boolean sucess = false;

            //if textbox is not autofilled with prno

           

            // only one row is present means no alteration done 
            if (tblpurchaseorder.RowCount == 1)
            {
                lblstatus.Text = "There is No  Purchase Data to Add";

            }
            else
            {

                for (int i = 0; i < tblpurchaseorder.RowCount - 1; i++)
                {

                    lblstatus.Text = "";

                    if (tblpurchaseorder.Rows[i].Cells[0].Value == null || tblpurchaseorder.Rows[i].Cells[0].Value.ToString().Trim() == "")
                    {

                        lblstatus.Text = "Please Enter Item in " + i + "th ROW";
                    }

                    else if (tblpurchaseorder.Rows[i].Cells[1].Value == null || tblpurchaseorder.Rows[i].Cells[1].Value.ToString().Trim() == "")
                    {
                        lblstatus.Text = "Please Enter Description in " + i + "th ROW";
                    }
                    else if (tblpurchaseorder.Rows[i].Cells[2].Value == null || tblpurchaseorder.Rows[i].Cells[2].Value.ToString().Trim() == "")
                    {
                        lblstatus.Text = "Please Enter Supplier in " + i + "th ROW";
                    }
                    else if (tblpurchaseorder.Rows[i].Cells[3].Value == null || tblpurchaseorder.Rows[i].Cells[3].Value.ToString().Trim() == "")
                    {
                        lblstatus.Text = "Please Enter Quantity in " + i + "th ROW";
                    }
                   
                    
                    
                    
                    else
                    {
                        sucess = true;
                    }
                }
                if (sucess == true)
                {
                    insertdatabean();


                }

            }



        }




        ///<summary>
        /// insertdata to a databean 
        /// </summary>
        public void insertdatabean()
        {

            if (tblpurchaseorder.RowCount == 1)
            {
                lblstatus.Text = "There is No  Purchase Data to Add";

            }
            else
            {
                for (int i = 0; i < tblpurchaseorder.RowCount - 1; i++)
                {
                    purchaserequestdatabean = new DataBean.PurchaseRequestDatabean();
                    purchaserequestdatabean.Prpk = int.Parse(tblpurchaseorder.Rows[i].Cells[0].Value.ToString());
                    purchaserequestdatabean.Prno = tblpurchaseorder.Rows[i].Cells[1].Value.ToString();
                    purchaserequestdatabean.Item = tblpurchaseorder.Rows[i].Cells[2].Value.ToString();
                    purchaserequestdatabean.Description = tblpurchaseorder.Rows[i].Cells[3].Value.ToString();
                    purchaserequestdatabean.Quantity = tblpurchaseorder.Rows[i].Cells[4].Value.ToString();
                    purchaserequestdatabean.Supplier = tblpurchaseorder.Rows[i].Cells[5].Value.ToString();
                   
                    
                    purchaserequestdatabean.Remark = tblpurchaseorder.Rows[i].Cells[6].Value.ToString();
                    purchaserequestdatabean.Prdate = (DateTime) tblpurchaseorder.Rows[i].Cells[7].Value;
                    purchaserequestdatabean.Isaproval = (Boolean)tblpurchaseorder.Rows[i].Cells[8].Value;
                   
                    purchaserequestdatabean.Aprovaldate = DateTime.Now.Date.ToString();
                    updateaction(purchaserequestdatabean);
                }
               
               
               
            }
        }

        private void btnapprove_Click(object sender, EventArgs e)
        {
            validateDatagrid();
        }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }
}
